<?php
/**
 * class Permission 
 */
define('SSLIB_Permission', TRUE);
//+++::users -rwxr--r--
//{name ::users 644}
//{/name}
function Iam($who){return Permission::CheckUser($who);}
class Permission
{
	static function CheckUser($val)
	{//TODO: optimization, optimization, and again optimization. amen.
		if(!array_key_exists(sslib_groups_names, $_ENV)) throw new Err(__T('groups names uninitialized'));
		if(!array_key_exists(sslib_groups_compiled, $_ENV)) throw new Err(__T('compiled groups uninitialized'));
		
		//list($user, $group) = 
		$arr = split('::', $val);
		
		$user = $arr[0];
		$group = (empty($arr[1])?'':$arr[1]);
		
		
		if(!array_key_exists($group, $_ENV[sslib_groups_names])) return false;
		$group = $_ENV[sslib_groups_names][$group];
		$user = trim($user);

		if($user)
			return (Text::Lower(Session::User()->Nick()) == Text::Lower($user));
		else 
		{
			
			$groups = $_ENV[sslib_groups_compiled];
			$usergroup = Session::User()->GroupID();
						
			if(!array_key_exists($group, $groups))
				return false;
			if($usergroup == $group) return true;
			$parent = $usergroup;//parent
			while(array_key_exists($parent, $groups))
			{
				$parent = $groups[$parent][1];
				if($parent == $group) return true;
			}
			return false;
		}
	}
}
?>